package com.project.easy.common.util.util;

import com.project.easy.common.util.collection.CollectionUtil;
import com.project.easy.common.util.constants.CommonConstant;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/9/5 21:08
 * @description: 获取请求参数
 */
public final class RequestUtil {
    /**
     * 私有构造方法
     */
    private RequestUtil() {
    }
    /**
     * 获取Request数据 同时可以根据字段Key
     * @param request 请求
     * @param field 字段信息
     * @return 数据
     */
   public static Map<String, String[]> parRequestParam(HttpServletRequest request, String ...field) {
        if (Objects.isNull(request)) {
            return CommonConstant.EMPTY_MAP;
        }
       Map<String, String[]> parameterMap = request.getParameterMap();
        if (!CollectionUtil.isNull(field)) {
            Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();
            List<String> strings = CollectionUtil.convertToArray(field);
            while (iterator.hasNext()) {
                Map.Entry<String, String[]> next = iterator.next();
                if (!strings.contains(next.getKey())) {
                    iterator.remove();
                }
            }
        }
        return  parameterMap;
   }
}
